/*
 * Created on 21/10/2005
 */
package bancosys.tec.persist.base.enumx;

import java.util.List;

import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.enums.Enum;

/**
 * Define um tipo especifico para dominios Fixos. Classe basica para ser extendida e colocar valores tipo par chave/valor aonde a chave eh
 * um int.
 * 
 * @author Daisy
 */
public abstract class IntValuedEnum extends Enum {

    /** Lang version 1.0.1 serial compatibility */
    private static final long serialVersionUID = -7129650521543789085L;

    /**
     * The value contained in enum.
     */
    private final int iValue;

    /**
     * Constructor for enum item.
     * 
     * @param name the name of enum item
     * @param value the value of enum item
     */
    protected IntValuedEnum(String name, int value) {
        super(name);
        this.iValue = value;
    }

    /**
     * <p>
     * Gets an <code>Enum</code> object by class and value.
     * </p>
     * <p>
     * This method loops through the list of <code>Enum</code>, thus if there are many <code>Enum</code>s this will be slow.
     * </p>
     * 
     * @param enumClass the class of the <code>Enum</code> to get
     * @param value the value of the <code>Enum</code> to get
     * @return the enum object, or null if the enum does not exist
     */

    public static Enum getEnumByValue(Class<?> enumClass, int value) {
        if (enumClass == null) {
            throw new IllegalArgumentException("The Enum Class must not be null");
        }
        @SuppressWarnings("unchecked")
        List<IntValuedEnum> list = Enum.getEnumList(enumClass);
        for (IntValuedEnum intValuedEnum : list) {
            if (intValuedEnum.getValue() == value) {
                return intValuedEnum;
            }
        }
        return null;
    }

    /**
     * <p>
     * Get value of enum item.
     * </p>
     * 
     * @return the enum item's value.
     */
    public final int getValue() {
        return this.iValue;
    }

    /**
     * <p>
     * Tests for order.
     * </p>
     * <p>
     * The default ordering is numeric by value, but this can be overridden by subclasses.
     * </p>
     * 
     * @see java.lang.Comparable#compareTo(Object)
     * @param other the other object to compare to
     * @return -ve if this is less than the other object, +ve if greater than, <code>0</code> of equal
     */
    @Override
    public int compareTo(Object other) {
        return this.iValue - ((IntValuedEnum) other).iValue;
    }

    /**
     * <p>
     * Human readable description of this <code>Enum</code> item.
     * </p>
     * 
     * @return String in the form <code>type[name=value]</code>, for example: <code>JavaVersion[Java 1.0=100]</code>. Note that the package
     * name is stripped from the type name.
     */
    @Override
    public String toString() {
        if (this.iToString == null) {
            String shortName = ClassUtils.getShortClassName(this.getEnumClass());
            this.iToString = shortName + "[" + this.getName() + "=" + this.getValue() + "]";
        }
        return this.iToString;
    }
}
